        .syntax unified  

        
#ifndef   __SUPPORT_RESET_HALT_AFTER_BTL
  #define __SUPPORT_RESET_HALT_AFTER_BTL  1
#endif
        
/*********************************************************************
*
*       Macros
*
**********************************************************************
*/
//
// Just place a vector (word) in the table
//
.macro VECTOR Name=
        .section .vectors, "a"
        .word \Name
.endm
//
// Declare an interrupt handler
//
.macro ISR_HANDLER Name=
        //
        // Insert vector in vector table
        //
        .section .vectors, "a"
        .word \Name
        //
        // Insert dummy handler in init section
        //
        .section .init.\Name, "ax"
        .thumb_func
        .weak \Name
        .balign 2
\Name:
        1: b 1b   // Endless loop
        END_FUNC \Name
.endm

//
// Place a reserved vector in vector table
//
.macro ISR_RESERVED
        .section .vectors, "a"
        .word 0
.endm

//
// Mark the end of a function and calculate its size
//
.macro END_FUNC name
        .size \name,.-\name
.endm

/***************************Vector Table*****************************/
        .section .vectors, "a"
        .code 16
        .balign 4
        .global _vectors
_vectors:
        VECTOR __stack_end__
        VECTOR Reset_Handler
        ISR_HANDLER NMI_Handler
        VECTOR HardFault_Handler
        ISR_RESERVED
        ISR_RESERVED
        ISR_RESERVED
        ISR_RESERVED
        ISR_RESERVED
        ISR_RESERVED
        ISR_RESERVED
        ISR_HANDLER SVC_Handler
        ISR_RESERVED
        ISR_RESERVED
        ISR_HANDLER PendSV_Handler
        ISR_HANDLER SysTick_Handler

        ISR_HANDLER IRQ000_Handler
        ISR_HANDLER IRQ001_Handler
        ISR_HANDLER IRQ002_Handler
        ISR_HANDLER IRQ003_Handler
        ISR_HANDLER IRQ004_Handler
        ISR_HANDLER IRQ005_Handler
        ISR_HANDLER IRQ006_Handler
        ISR_HANDLER IRQ007_Handler
        ISR_HANDLER IRQ008_Handler
        ISR_HANDLER IRQ009_Handler
        ISR_HANDLER IRQ010_Handler
        ISR_HANDLER IRQ011_Handler
        ISR_HANDLER IRQ012_Handler
        ISR_HANDLER IRQ013_Handler
        ISR_HANDLER IRQ014_Handler
        ISR_HANDLER IRQ015_Handler
        ISR_HANDLER IRQ016_Handler
        ISR_HANDLER IRQ017_Handler
        ISR_HANDLER IRQ018_Handler
        ISR_HANDLER IRQ019_Handler
        ISR_HANDLER IRQ020_Handler
        ISR_HANDLER IRQ021_Handler
        ISR_HANDLER IRQ022_Handler
        ISR_HANDLER IRQ023_Handler
        ISR_HANDLER IRQ024_Handler
        ISR_HANDLER IRQ025_Handler
        ISR_HANDLER IRQ026_Handler
        ISR_HANDLER IRQ027_Handler
        ISR_HANDLER IRQ028_Handler
        ISR_HANDLER IRQ029_Handler
        ISR_HANDLER IRQ030_Handler
        ISR_HANDLER IRQ031_Handler

        .section .vectors, "a"
        .size _vectors, .-_vectors
_vectors_end:

/*************************Global functions***************************/

/*********************************************************************
*       Reset_Handler
*/
        .global reset_handler
        .global Reset_Handler
        .equ reset_handler, Reset_Handler
        .section .init.Reset_Handler, "ax"
        .balign 2
        .thumb_func
Reset_Handler:
#if __SUPPORT_RESET_HALT_AFTER_BTL != 0
        movs    R0, #8
        ldr     R0, [R0]
        nop
        nop
#endif
IAR_STARTUP_CODE_START:
        // reset NVIC if in rom debug
        ldr     R0, =0x20000000
        ldr     R2, =0x0      // vector offset
        mov     R1, PC
        cmp     R1, R0
        bls     ROMCODE
        
        // ram code base address. 
        add     R2, R0,R2
ROMCODE:
        // reset Vector table address.
        ldr     R0, =0xE000ED08
        str     R2, [R0]
IAR_STARTUP_CODE_END:
        // Call SystemInit
        bl      SystemInit
        // Call runtime initialization, which calls main().
        bl      _start
END_FUNC Reset_Handler

        //
        // Weak only declaration of SystemInit enables Linker to replace bl SystemInit with a NOP,
        // when there is no strong definition of SystemInit.
        //
        .weak SystemInit

/*********************************************************************
*       HardFault_Handler
*/
        .weak HardFault_Handler
        .section .init.HardFault_Handler, "ax"
        .balign 2
        .thumb_func
HardFault_Handler:
        b       .
END_FUNC HardFault_Handler

/*************************** End of file ****************************/
